<!DOCTYPE html>
<html>
<body>
    <h1>JavaScript 闭包</h1>
    <p>使用局部变量计数。</p>
    <button type="button" onclick="myFunction()">计数！</button>
    <p id="demo">0</p>
    <script>
        "use strict";
        let add = (function () {
            let counter = 0;
            return function () {
                counter += 1;
                console.log(counter)
                return counter;
            }
        })();

        function myFunction() {
            document.getElementById("demo").innerHTML = add();
        }
    </script>
</body>
</html>